package cn.iocoder.yudao.module.erp.controller.admin.business.produce.defectiveitems;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.produce.defectiveitems.vo.DefectiveItemsPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.produce.defectiveitems.vo.DefectiveItemsRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.produce.defectiveitems.vo.DefectiveItemsSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.produce.defectiveitems.DefectiveItemsDO;
import cn.iocoder.yudao.module.erp.service.business.produce.defectiveitems.DefectiveItemsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 不良品项")
@RestController
@RequestMapping("/erp/defective-items")
@Validated
public class DefectiveItemsController {

    @Resource
    private DefectiveItemsService defectiveItemsService;

    @PostMapping("/create")
    @Operation(summary = "创建不良品项")
    @PreAuthorize("@ss.hasPermission('erp:defective-items:create')")
    public CommonResult<Long> createDefectiveItems(@Valid @RequestBody DefectiveItemsSaveReqVO createReqVO) {
        return success(defectiveItemsService.createDefectiveItems(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新不良品项")
    @PreAuthorize("@ss.hasPermission('erp:defective-items:update')")
    public CommonResult<Boolean> updateDefectiveItems(@Valid @RequestBody DefectiveItemsSaveReqVO updateReqVO) {
        defectiveItemsService.updateDefectiveItems(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除不良品项")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:defective-items:delete')")
    public CommonResult<Boolean> deleteDefectiveItems(@RequestParam("id") Long id) {
        defectiveItemsService.deleteDefectiveItems(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得不良品项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:defective-items:query')")
    public CommonResult<DefectiveItemsRespVO> getDefectiveItems(@RequestParam("id") Long id) {
        DefectiveItemsDO defectiveItems = defectiveItemsService.getDefectiveItems(id);
        return success(BeanUtils.toBean(defectiveItems, DefectiveItemsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得不良品项分页")
    @PreAuthorize("@ss.hasPermission('erp:defective-items:query')")
    public CommonResult<PageResult<DefectiveItemsRespVO>> getDefectiveItemsPage(@Valid DefectiveItemsPageReqVO pageReqVO) {
        PageResult<DefectiveItemsDO> pageResult = defectiveItemsService.getDefectiveItemsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DefectiveItemsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出不良品项 Excel")
    @PreAuthorize("@ss.hasPermission('erp:defective-items:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDefectiveItemsExcel(@Valid DefectiveItemsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DefectiveItemsDO> list = defectiveItemsService.getDefectiveItemsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "不良品项.xls", "数据", DefectiveItemsRespVO.class,
                        BeanUtils.toBean(list, DefectiveItemsRespVO.class));
    }

}